/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.pms.service;

import com.fzhucc.modules.pms.domain.ProjectNotice;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.pms.repository.ProjectNoticeRepository;
import com.fzhucc.modules.pms.domain.dto.ProjectNoticeDto;
import com.fzhucc.modules.pms.domain.dto.ProjectNoticeQueryCriteria;
import com.fzhucc.modules.pms.service.mapstruct.ProjectNoticeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2022-11-30
**/
@Service
@RequiredArgsConstructor
public class ProjectNoticeService {

    private final ProjectNoticeRepository projectNoticeRepository;
    private final ProjectNoticeMapper projectNoticeMapper;

    public Map<String,Object> queryAll(ProjectNoticeQueryCriteria criteria, Pageable pageable){
        Page<ProjectNotice> page = projectNoticeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(projectNoticeMapper::toDto));
    }

    public List<ProjectNoticeDto> queryAll(ProjectNoticeQueryCriteria criteria){
        return projectNoticeMapper.toDto(projectNoticeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    public ProjectNoticeDto findById(Integer id) {
        ProjectNotice projectNotice = projectNoticeRepository.findById(id).orElseGet(ProjectNotice::new);
        ValidationUtil.isNull(projectNotice.getId(),"ProjectNotice","id",id);
        return projectNoticeMapper.toDto(projectNotice);
    }

    @Transactional(rollbackFor = Exception.class)
    public ProjectNoticeDto create(ProjectNotice resources) {
        return projectNoticeMapper.toDto(projectNoticeRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(ProjectNotice resources) {
        ProjectNotice projectNotice = projectNoticeRepository.findById(resources.getId()).orElseGet(ProjectNotice::new);
        ValidationUtil.isNull( projectNotice.getId(),"ProjectNotice","id",resources.getId());
        projectNotice.copy(resources);
        projectNoticeRepository.save(projectNotice);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            projectNoticeRepository.deleteById(id);
        }
    }

    public void download(List<ProjectNoticeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProjectNoticeDto projectNotice : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", projectNotice.getUserId());
            map.put("项目id", projectNotice.getProjectId());
            map.put("内容", projectNotice.getMsg());
            map.put("发生时间", projectNotice.getHappenTime());
            //map.put("是否展示 默认展示", projectNotice.getIsShow());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}